import { readFileSync } from 'fs';
import { join } from 'path';

async function testBulkImport() {
  try {
    // 读取种子数据
    const dataPath = join(process.cwd(), 'data', 'customers-seed.json');
    const customerData = JSON.parse(readFileSync(dataPath, 'utf8'));
    
    // 测试前5个客户
    const testData = customerData.slice(0, 5);
    
    console.log('测试批量导入客户数据...');
    console.log(`准备导入 ${testData.length} 个客户:`);
    
    testData.forEach((customer, index) => {
      console.log(`${index + 1}. ${customer.name} - ${customer.email}`);
    });
    
    // 发送请求到批量导入API
    const response = await fetch('http://localhost:3000/api/customers/bulk', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(testData),
    });
    
    const result = await response.json();
    
    if (response.ok) {
      console.log('✅ 批量导入成功!');
      console.log(`导入数量: ${result.importedCount}`);
      console.log('导入的客户:', result.customers);
    } else {
      console.error('❌ 批量导入失败:', result.error);
      if (result.duplicateEmails) {
        console.log('重复的邮箱:', result.duplicateEmails);
      }
    }
    
  } catch (error) {
    console.error('测试脚本执行失败:', error);
  }
}

// 运行测试
testBulkImport().catch(console.error);